import 'package:flutter/material.dart';
import 'package:hxdj/common/base_page/my_loading_page.dart';
import 'package:hxdj/common/components/my_tap_widget.dart';
import 'package:hxdj/common/components/my_text.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/home/address/edit_address/edit_address_page.dart';
import 'package:hxdj/home/address/select_address/address_model.dart';
import 'package:hxdj/home/address/select_address/select_address_viewmodel.dart';
import 'package:hxdj/home/home_viewmodel.dart';
import 'package:hxdj/home/order/order_create_viewmodel.dart';
import 'package:provider/provider.dart';

//选择地址
class SelectAddressPage extends StatefulWidget {
  final String form; //来自哪里

  static String routeName = "/SelectAddressPage";

  const SelectAddressPage({super.key, required this.form});

  @override
  State<SelectAddressPage> createState() => _SelectAddressPageState();
}

class _SelectAddressPageState extends State<SelectAddressPage> {
  late SelectAddressViewModel viewModel;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel = context.read<SelectAddressViewModel>();
      viewModel.requestData();
    });
  }

  _buildBody() {
    switch (viewModel.state) {
      case ViewState.loading:
        return const MyLoadingWidget();
      default:
        return SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: MyColours.materialBg,
              //设置四周圆角 角度
              borderRadius: BorderRadius.circular(10),
            ),
            margin: const EdgeInsets.only(left: 10, right: 10, top: 20),
            child: Column(
              children: [
                Expanded(
                    child: ListView.builder(
                        itemBuilder: (context, index) {
                          AddressModel addressModel = viewModel.addressModelsDataList[index];
                          return MyTapWidget(
                              onTap: () {
                                //返回上个页面
                                NavigatorUtils.goBack(context);
                                if (widget.form == "home") {
                                  HomeViewModel homeViewModel = Provider.of<HomeViewModel>(context, listen: false);
                                  homeViewModel.setAddress(addressModel);
                                } else {
                                  OrderCreateViewModel orderCreateViewModel =
                                      Provider.of<OrderCreateViewModel>(context, listen: false);
                                  orderCreateViewModel.selectServiceAddress(addressModel);
                                }
                              },
                              child: AddressItem(addressModel: addressModel));
                        },
                        itemCount: viewModel.addressModelsDataList.length)),
                GestureDetector(
                  onTap: () async {
                    NavigatorUtils.pushParams(context, EditAddressPage.routeName);
                  },
                  child: Container(
                    margin: const EdgeInsets.only(top: 30, bottom: 30),
                    padding: const EdgeInsets.symmetric(horizontal: 40),
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 15),
                      decoration: BoxDecoration(
                        color: MyColours.appMain,
                        //设置四周圆角 角度
                        borderRadius: BorderRadius.circular(30),
                      ),
                      alignment: Alignment.center,
                      child: const Text("新建服务地址",
                          style: TextStyle(fontSize: 15, color: Colors.white, fontWeight: FontWeight.w700)),
                    ),
                  ),
                )
              ],
            ),
          ),
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (context, SelectAddressViewModel? model, Widget? child) {
      viewModel = model!;
      return Scaffold(
        backgroundColor: MyColours.bgColor,
        appBar: const MyAppBar(title: "选择地址"),
        body: _buildBody(),
      );
    });
  }
}

class AddressItem extends StatelessWidget {
  const AddressItem({super.key, required this.addressModel});

  final AddressModel addressModel;
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 10.w, right: 10.w, top: 15.h),
      child: Row(
        children: [
          //头像
          MyBorder(
              radius: 20.w,
              backgroundColor: MyColours.appMain.withOpacity(0.2),
              child: Container(
                alignment: Alignment.center,
                width: 40.w,
                height: 40.w,
                child: MyText(
                  addressModel.contacts?.substring(0, 1) ?? "欢",
                  color: MyColours.appMain,
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w600,
                ),
              )),
          //地址信息
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: 12.5.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      MyText(addressModel.contacts ?? "", textStyle: MyTextStyles.textBold14),
                      MyGaps.hGap8,
                      MyText(
                        addressModel.mobile ?? "",
                        color: MyColours.text,
                        fontSize: 12.sp,
                        fontWeight: FontWeight.w400,
                      ),
                      MyGaps.hGap15,
                      addressModel.defaultStatus == 1
                          ? MyBorder(
                              width: 1,
                              color: MyColours.appMain,
                              radius: 3,
                              child: Container(
                                padding: const EdgeInsets.all(3.0),
                                child: MyText("默认地址", color: MyColours.appMain, fontSize: 10.sp),
                              ))
                          : Container()
                    ],
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 10.h),
                    child: Text(
                      addressModel.address! + (addressModel.addressDetail ?? ""),
                      maxLines: 5,
                      style: TextStyle(color: MyColours.text, fontSize: 12.sp, overflow: TextOverflow.ellipsis),
                    ),
                  ),
                ],
              ),
            ),
          ),
          //编辑按钮
          GestureDetector(
              onTap: () {
                NavigatorUtils.pushParams(context, '${EditAddressPage.routeName}?addressId=${addressModel.id}');
              },
              child: Container(padding: EdgeInsets.all(10.w), child: MyImage(imageName: "ic_edit", width: 15.w))),
        ],
      ),
    );
  }
}
